package com.qozix.tileview.plugins;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qozix.tileview.TileView;
import com.stickerview.view.StickerImageView;

// TODO: recycling
public class MarkerPlugin extends ViewGroup implements TileView.Plugin, TileView.Listener {

  private float mScale = 1;

  public MarkerPlugin(@NonNull Context context) {
    super(context);
  }

  @Override
  public void install(TileView tileView) {
    tileView.addListener(this);
    tileView.addView(this);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    measureChildren(widthMeasureSpec, heightMeasureSpec);
    for (int i = 0; i < getChildCount(); i++) {
      View child = getChildAt(i);
      double[] coordinate = (double[]) child.getTag();
      child.setX((float) coordinate[0]*mScale);
      child.setY((float)coordinate[1]*mScale);
//         LayoutParams layoutParams = populateLayoutParams(child);
//        child.setLeft(layoutParams.mLeft);
//        child.setRight(layoutParams.mRight);
//        child.setTop(layoutParams.mTop);
//        child.setBottom(layoutParams.mBottom);



    }
    int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
    int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(availableWidth, availableHeight);
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    for (int i = 0; i < getChildCount(); i++) {
      View child = getChildAt(i);
      if (child.getVisibility() != GONE) {
          double[] coordinate = (double[]) child.getTag();
          child.setX((float) coordinate[0]*mScale);
          child.setY((float)coordinate[1]*mScale);
        //LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
        //child.layout(layoutParams.mLeft, layoutParams.mTop, layoutParams.mRight, layoutParams.mBottom);
      }
    }
  }

  private LayoutParams populateLayoutParams(View child) {
      LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
      if(child.getVisibility()!=View.GONE) {
        int actualWidth = child.getWidth();
        int actualHeight = child.getHeight();

        float widthOffset = actualWidth * layoutParams.relativeAnchorX + layoutParams.absoluteAnchorX;
        float heightOffset = actualHeight * layoutParams.relativeAnchorY + layoutParams.absoluteAnchorY;

        double[] coordinate = (double[]) child.getTag();

        if(child instanceof StickerImageView) {
          Log.e("xy_scale_1_缩放", coordinate[0] + "/" + coordinate[1]);
        }

        int scaledX = (int) (coordinate[0] * mScale);
        int scaledY = (int) (coordinate[1] * mScale);

        layoutParams.mLeft = (int) (scaledX + widthOffset);
        layoutParams.mTop = (int) (scaledY + heightOffset);
        layoutParams.mRight = layoutParams.mLeft + actualWidth;
        layoutParams.mBottom = layoutParams.mTop + actualHeight;
        Log.e("layout1", layoutParams.mLeft + "/" + layoutParams.mTop);
      }
    return layoutParams;
  }




  private void reposition() {
    for (int i = 0; i < getChildCount(); i++) {
      View child = getChildAt(i);
      if (child.getVisibility() != GONE) {
        double[] coordinate = (double[]) child.getTag();
        child.setX((float) coordinate[0]*mScale);
        child.setY((float)coordinate[1]*mScale);
//          LayoutParams layoutParams = populateLayoutParams(child);
//          child.setLeft(layoutParams.mLeft);
//          child.setRight(layoutParams.mRight);
//          child.setTop(layoutParams.mTop);
//          child.setBottom(layoutParams.mBottom);
      }
    }
  }

  @Override
  public void onScaleChanged(float scale, float previous) {
    mScale = scale;
    reposition();
  }

  public void addMarker(View view, int left, int top, float relativeAnchorLeft, float relativeAnchorTop, float absoluteAnchorLeft, float absoluteAnchorTop) {
//    LayoutParams layoutParams = new MarkerPlugin.LayoutParams(
//        LayoutParams.WRAP_CONTENT,
//        LayoutParams.WRAP_CONTENT,
//        left, top,
//        relativeAnchorLeft, relativeAnchorTop,
//        absoluteAnchorLeft, absoluteAnchorTop);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
    );

    layoutParams.gravity = Gravity.CENTER;

    view.setX(left);
    view.setY(top);
    addView(view, layoutParams);

  }

  public static class LayoutParams extends ViewGroup.LayoutParams {

    public int x;
    public int y;
    public float relativeAnchorX;
    public float relativeAnchorY;
    public float absoluteAnchorX;
    public float absoluteAnchorY;

    private int mTop;
    private int mLeft;
    private int mBottom;
    private int mRight;

    public LayoutParams(int width, int height, int left, int top, float relativeAnchorLeft, float relativeAnchorTop, float absoluteAnchorLeft, float absoluteAnchorTop) {
      super(width, height);
      x = left;
      y = top;
      relativeAnchorX = relativeAnchorLeft;
      relativeAnchorY = relativeAnchorTop;
      absoluteAnchorX = absoluteAnchorLeft;
      absoluteAnchorY = absoluteAnchorTop;
    }

  }

}
